package com.yhl.file;


import org.springframework.stereotype.Component;
import org.springframework.stereotype.Service;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.apache.poi.xwpf.usermodel.*;
import com.itextpdf.text.Document;
import com.itextpdf.text.DocumentException;
import com.itextpdf.text.Paragraph;
import com.itextpdf.text.pdf.PdfWriter;


import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;

/**
 * @author: yanghl
 * @description: TODO
 * @date: 2024/7/2 15:55
 */
@Service
@Component
public class FileServices {

    public void generateExcel(){
        Workbook workbook = new XSSFWorkbook();
        Sheet sheet = workbook.createSheet("Example Sheet");

        // 创建表头
        Row headerRow = sheet.createRow(0);
        Cell headerCell = headerRow.createCell(0);
        headerCell.setCellValue("Name");
        headerCell = headerRow.createCell(1);
        headerCell.setCellValue("Age");

        // 表体数据
        Row dataRow = sheet.createRow(1);
        Cell dataCell = dataRow.createCell(0);
        dataCell.setCellValue("John Doe");
        dataCell = dataRow.createCell(1);
        dataCell.setCellValue(30);

        try (FileOutputStream fileOut = new FileOutputStream("example.xlsx")) {
            workbook.write(fileOut);
        } catch (IOException e) {
            e.printStackTrace();
        }

        try {
            workbook.close();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }

    // 创建world文档
    public void generateWorld(){
        // 创建一个新的文档
        XWPFDocument document = new XWPFDocument();
        // 创建一级标题
        createTitle(document, "一级标题", "Heading1");
        // 创建二级标题
        createTitle(document, "二级标题", "Heading2");
        // 创建三级标题
        createTitle(document, "三级标题", "Heading3");
        // 创建正文
        createParagraph(document, "这是正文内容。");
        // 将文档写入文件
        try (FileOutputStream out = new FileOutputStream("GeneratedDocument.docx")) {
            document.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    // 创建标题
    private static void createTitle(XWPFDocument document, String text, String style) {
        XWPFParagraph paragraph = document.createParagraph();
        paragraph.setStyle(style);

        // 居中
        // paragraph.setAlignment(ParagraphAlignment.CENTER);
        XWPFRun run = paragraph.createRun();
        run.setText(text);
    }
    // 创建正文
    private static void createParagraph(XWPFDocument document, String text) {
        XWPFParagraph paragraph = document.createParagraph();
        XWPFRun run = paragraph.createRun();
        run.setText(text);
    }

    public void generateForLeaveWorld(String fileName){
        String name = "陈东琳";
        LocalDate date = LocalDate.now(); // 获取当前日期
        String formattedDate = date.format(DateTimeFormatter.ofPattern("yyyy年MM月dd日"));
        String leaveApplication = generateLeaveApplication(name, formattedDate);
        // 创建一个新的文档
        XWPFDocument document = new XWPFDocument();
        // 把数据写入文档
        createParagraph(document, leaveApplication);
        // 将文档写入文件
        try (FileOutputStream out = new FileOutputStream(fileName)) {
            document.write(out);
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    // 请假条
    public static String generateLeaveApplication(String name, String date) {
        return "请假条\n\n" +
                "尊敬的领导：\n\n" +
                "    您好！我是" + name + "，由于个人原因，需请假数日。在此特向您请假，希望能得到您的批准。\n\n" +
                "    我计划从" + date + "开始请假，预计请假时间为三天。在请假期间，我将确保工作交接完毕，不影响工作的正常进行。\n\n" +
                "    请假期间如有紧急事项需要处理，我会保持手机畅通，并尽量协调解决。\n\n" +
                "    感谢您的理解与支持。\n\n" +
                "此致\n\n" +
                "敬礼\n\n" +
                name + "\n" +
                date + "\n";
    }

    public void generatePdf(){
        Document document = new Document();
        try {
            PdfWriter.getInstance(document, new FileOutputStream("example.pdf"));
            document.open();
            document.add(new Paragraph("Hello, this is a sample PDF document!"));
        } catch (DocumentException | FileNotFoundException e) {
            e.printStackTrace();
        } finally {
            document.close();
        }
    }


}

